#include "mainscene.h"
#include "ui_mainscene.h"
#include <QPainter>
#include <QDebug>
#include <QTimer>
#include <QSound>

MainScene::MainScene(QWidget *parent): QMainWindow(parent), ui(new Ui::MainScene)
{
    ui->setupUi(this);

    //配置主场景

    //设置固定大小
    setFixedSize(320,588);

    //设置图标
    setWindowIcon(QIcon(":/res/Coin0001.png"));

    //设置标题
    setWindowTitle("翻转金币主场景");

    //退出按钮实现
    connect(ui->actionquit,&QAction::triggered,[=](){
        this->close();
    });

    QSound *startSound = new QSound(":/res/TapButtonSound.wav",this);


    //开始按钮
    MyPushButton* startBtn = new MyPushButton(":/res/MenuSceneStartButton.png");
    startBtn->setParent(this);
    startBtn->move(this->width()*0.5 - startBtn->width()*0.5,this->height()*0.7);

    //选择关卡场景
    chooseScene = new ChooseLevelScene;

    //监听点击事件，执行特效
    connect(startBtn,&MyPushButton::clicked,[=](){
       startSound->play(); //开始音效
       startBtn->zoom1();//向下跳跃
       startBtn->zoom2();//向上跳跃
       //延时0.5秒后 进入选择场景
       QTimer::singleShot(500,this,[=](){
           //设置chooseScene情景位置
          chooseScene->setGeometry(this->geometry());
          this->hide();
          chooseScene->show();
       });
    });


    //监听选择场景的返回按钮
    connect(chooseScene,&ChooseLevelScene::chooseSceneBack,[=](){
        this->setGeometry(chooseScene->geometry());//切换场景后位置保持一致
        chooseScene->hide();//将选择关卡场景 隐藏掉
        this->show();//重新显示主场景
    });
}

//重写MainScene的PaintEvent事件
void MainScene::paintEvent(QPaintEvent *)
{
    //创建画家,指定绘图设备
    QPainter painter(this);
    //创建Qpixmap对象
    QPixmap pix;
    //加载图片
    pix.load(":/res/PlayLevelSceneBg.png");
    //绘制背景图
    painter.drawPixmap(0,0,this->width(),this->height(),pix);

    //加载标题
    pix.load(":/res/Title.png");
    //缩放图片
    pix = pix.scaled(pix.width()*0.5,pix.height()*0.5);
    //绘制标题
    painter.drawPixmap(10,30,pix.width(),pix.height(),pix);

}

MainScene::~MainScene()
{
    delete ui;
}

